<?php

namespace admin\controllers;

use app\models\Post;
use admin\components\AdminController;
use app\utils\BaseUtils;
use Yii;
use yii\imagine\Image;

class PostController extends AdminController {

    public $name = '文章管理';

    public function actionIndex($cid = Post::CID_TECH) {
        $searchModel = !$this->enableSearch ? NULL : new Post();
        $query = Post::find()->where(['cid' => $cid]);
        $dataProvider = $this->search($query, $searchModel);
        return $this->render(compact('searchModel', 'dataProvider'));
    }

    public function actionSave($id = FALSE) {
        /* @var $model Post */
        $model = $this->findModel($id, Post::className());
        if (!$model->load($this->post())) {
            return $this->render(compact('model'));
        }
        $model->type = Post::TYPE_POST;

        if (!empty($model->thumbnail)) {
            BaseUtils::thumb(APP_ROOT . $model->thumbnail, 800, 800);
            BaseUtils::watermark(APP_ROOT . $model->thumbnail);
        }
        return $model->save();
    }

    public function actions() {
        return [
            'delete' => [
                'class' => 'admin\components\DeleteAction',
                'modelClass' => Post::className(),
                'status' => Post::STATUS_NO
            ]
        ];
    }
}
